package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.exception.MarketValidationException;
import com.cskaoyan.service.admin.AdminBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import com.cskaoyan.util.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author :rain
 * @description ?page=1&limit=20&sort=add_time&WXUserIndexVo=desc
 * @since 2022/06/26 20:26
 */
@RestController
@RequestMapping("admin/brand")
public class AdminBrandController {

    @Autowired
    AdminBrandService adminBrandService;

    /**
     * @param id   brand页面的商品id
     * @param name brand页面的商品name 模糊查询
     * @param info 分页的信息
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo list(String id, String name, BasePageInfo info) {
        CommonData<MarketBrand> data = adminBrandService.queryAllBrandInfo(id, name, info);
        if (data == null) {
            return BaseRespVo.invalidParameter("参数异常");
        }
        return BaseRespVo.ok(data);
    }

    /**
     * @param marketBrand 请求的是json字符串
     * @return
     */
    @RequestMapping("update")
    public BaseRespVo update(@Validated @RequestBody MarketBrand marketBrand, BindingResult bindingResult) throws MarketValidationException {
        ValidationUtils.valid(bindingResult);
        MarketBrand data = adminBrandService.modifyMarkBrand(marketBrand);
        return BaseRespVo.ok(data);
    }

    /**
     * @param marketBrand 请求参数有四个，分别是desc, floorPrice,name,picUrl 都是markedBrand的成员变量，直接用对象接受即可
     * @return
     */
    @RequestMapping("create")
    public BaseRespVo create(@Validated @RequestBody MarketBrand marketBrand, BindingResult bindingResult) throws MarketValidationException {
        ValidationUtils.valid(bindingResult);
        MarketBrand marketBrand1 = adminBrandService.insertNewInfo(marketBrand);
        return BaseRespVo.ok(marketBrand1);
    }

    /**
     * @param marketBrand 删除方法传过来的是个对象，根据传过来的信息直接删除即可
     * @return
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketBrand marketBrand) {
        Integer delete = adminBrandService.delete(marketBrand);
        if (delete == 200) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidData();

    }
}
